require 'digest/md5'
class User < ActiveRecord::Base
 attr_accessible :email, :password, :password_confirmation
  
  attr_accessor :password
  before_save :encrypt_password
  
  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email
  
  def self.authenticate(email, password)
    user = find_by_email(email)
	
	salt =  Digest::MD5.hexdigest("aB1cD2eF3G");
		
    if user && user.password_hash == Digest::MD5.hexdigest(password+salt)
      user
    else
      nil
    end
  end
  

  def encrypt_password
    if password.present?
		
      self.password_salt = Digest::MD5.hexdigest("aB1cD2eF3G");
      self.password_hash = Digest::MD5.hexdigest(password+password_salt)
    end
  end
end
